<?php

include '../DB/dbConnection.php';

$result=array();

if ($_POST){
	$id=0;
	$e="";
	
	$passwd=generateStrongPassword();
	
	$stmt = $conn->prepare("INSERT INTO student (id,roll_number,name,gender,blood,standard_id,division_id,height,weight,father_name,father_qualification,father_occupation,mother_name,mother_qualification,mother_occupation,guardian_name,guardian_qualification,guardian_occupation,city,village,email,district,landline,state,cell_number,address,image,passwd,school_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
	$stmt->bind_param("iisssiisssssssssssssssssssssi",$id,$_POST['roll_number'],$_POST['name'],$_POST['gender'],$e,$_POST['standard_id'],$_POST['division_id'],$e,$e,$e,$e,$e,$e,$e,$e,$e,$e,$e,$e,$e,$_POST['email'],$e,$e,$e,$_POST['cell_number'],$e,$e,$passwd,$_POST['school_id']);
	$success = $stmt->execute();
	$stmt->close();
	$conn->close();
	
	if($success)
		$result['error']='false';
	else
		$result['error']='true';
}
else
{
	$result['error']='true';
}

echo json_encode($result);

function generateStrongPassword($length = 8, $add_dashes = false, $available_sets = 'luds')
{
	$sets = array();
	if(strpos($available_sets, 'l') !== false)
		$sets[] = 'abcdefghjkmnpqrstuvwxyz';
	if(strpos($available_sets, 'u') !== false)
		$sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ';
	if(strpos($available_sets, 'd') !== false)
		$sets[] = '23456789';
	//if(strpos($available_sets, 's') !== false)
		//$sets[] = '!@#$%&*?';

	$all = '';
	$password = '';
	foreach($sets as $set)
	{
		$password .= $set[array_rand(str_split($set))];
		$all .= $set;
	}

	$all = str_split($all);
	for($i = 0; $i < $length - count($sets); $i++)
		$password .= $all[array_rand($all)];

	$password = str_shuffle($password);

	if(!$add_dashes)
		return $password;

	$dash_len = floor(sqrt($length));
	$dash_str = '';
	while(strlen($password) > $dash_len)
	{
		$dash_str .= substr($password, 0, $dash_len) . '-';
		$password = substr($password, $dash_len);
	}
	$dash_str .= $password;
	return $dash_str;
}
?> 
